package com.wangsd.web.api;

import com.wangsd.common.basicbean.Result;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.web.MyController;
import com.wangsd.web.face.entity.Member;
import com.wangsd.web.face.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Api(value = "api member", tags = "会员中心")
@Controller
@RequestMapping("/api/member")
@Slf4j
public class ApiMemberController extends MyController {

    @Autowired
    MemberService memberService;
    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation(value = "会员首页", notes = "会员首页", httpMethod = "GET")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public Object index(Long merchantId, String openId, String subOpenId, Model model) {
        Member member = memberService.selectByMerchantIdAndOpenId(merchantId, openId, subOpenId);
        if (member.getStatus() == 1) {//已经成为会员
            String mobile = member.getMobile();
            String end = mobile.substring(mobile.length() - 4);
            String befor = mobile.substring(0, 3);
            member.setMobile(befor + "****" + end);
            model.addAttribute("member", member);
            return "member";
        } else {//新用户，注册界面
            model.addAttribute("member", member);
            return "regist";
        }
    }

    /**
     * 激活会员
     *
     * @param merchantId
     * @param openId
     * @param subOpenId
     * @param nickName
     * @param mobile
     * @param msgcode
     * @return
     */
    @ApiOperation(value = "会员注册", notes = "会员注册", httpMethod = "POST")
    @RequestMapping(value = "/registMember", method = RequestMethod.POST)
    @ResponseBody
    public Object registMember(Long merchantId, String openId, String subOpenId,
                               String nickName, String mobile, String msgcode, String birthday) {
        Object o = redisTemplate.opsForValue().get(mobile);
        if (!msgcode.equals(o)) {
            return Result.fail("验证码不正确！");
        }
        Member member = memberService.selectByMerchantIdAndMobileAndStatus(merchantId, mobile, 1);
        if (member != null) {//已激活其他渠道会员
            member.setNickName(nickName);
            if (birthday != null)
                member.setBirthday(DateUtils.parseDate(birthday, "yyyy-MM-dd"));
            member.setOpenId(openId);
            member.setSubOpenId(subOpenId);

            Member member1 = memberService.selectByMerchantIdAndOpenId(merchantId, openId, subOpenId);
            if (member.getId() != member1.getId()) {
                memberService.removeById(member1.getId());
            }
        } else {//未激活其他渠道会员
            member = memberService.selectByMerchantIdAndOpenId(merchantId, openId, subOpenId);
            member.setNickName(nickName);
            if (birthday != null)
                member.setBirthday(DateUtils.parseDate(birthday, "yyyy-MM-dd"));
            member.setMobile(mobile);
            member.setStatus(1);
            member.setUseTime(new Date());
            //生成会员卡号 exp：2018000088000088
            String begin = DateUtils.formatDate(new Date(), "yyyy");
            String dft = "000000";
            String second = dft.substring(0, 6 - (merchantId.toString()).length()) + merchantId;
            String end = dft.substring(0, 6 - (member.getId().toString()).length()) + member.getId();
            member.setCardNo(begin + second + end);
        }
        memberService.saveOrUpdate(member);
        return Result.success();
    }

}
